<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 登录表单 -->
      <el-form
        class="fom_login"
        :model="loginForm"
        :rules="loginRules"
        ref="loginFormRules"
      >
        <el-form-item label="账号名字：" prop="username">
          <el-input
            prefix-icon="el-icon-s-custom"
            v-model="loginForm.username"
          ></el-input>
        </el-form-item>
        <el-form-item label="账号密码：" prop="password">
          <el-input
            type="password"
            prefix-icon="el-icon-unlock"
            v-model="loginForm.password"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="primary" @click="resetFiel">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loginForm: {
        username: "1234",
        password: "123456",
      },
      loginRules: {
        username: [
          { required: true, message: "请输入昵称", trigger: "blur" },
          { min: 2, max: 5, message: "长度在 2 到 5 个字符", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 4, max: 6, message: "长度在 4到 6 个字符", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    // 表单重置表单
    resetFiel() {
      this.$refs.loginFormRules.resetFields();
    },
    // 登录按钮
    login() {
      this.$refs.loginFormRules.validate(async (valid) => {
        if (!valid) return;
        const { data: res } = await this.$http.post("login", this.loginForm);
        if (res.meta.status == 200) {
          this.$message({
            message: "登录成功！",
            type: "success",
          });
        } else {
          this.$message.error("登录失败");
        }
        //token验证
        window.sessionStorage.setItem("token", res.data.token);
        this.$router.push("/home");
      });
    },
  },
};
</script>
<style lang="less" scoped>
.login_container {
  position: relative;
  background-color: #bbffff;
  width: 100%;
  height: 100%;
  position: fixed;
}
.login_box {
  position: absolute;
  left: 30%;
  top: 25%;
  background-color: white;
  height: 300px;
  width: 450px;
  padding: 0px 20px;
}
</style>
